#!usr/bin/python
# coding=utf-8

import time


class guide(object):
    open_wifi_settings = 'android.miui:id/open_wifi_settings_button'
    open_wifi_settings_btn_text = '打开Wifi设置界面'
    verified_btn = 'android.miui:id/action_button'
    verified_btn_text = '开始验证'
    wifi_name = 'MIOffice-2.4G'
    wifi_username_input = 'com.android.settings:id/identity'
    wifi_password_input = 'com.android.settings:id/password'
    wifi_submit = 'android:id/button1'
    wifi_settings_page_back = 'miui:id/up'
    mi_account_name = 'com.xiaomi.account:id/et_account_name'
    mi_account_password = 'com.xiaomi.account:id/et_account_password'
    mi_account_submit = 'com.xiaomi.account:id/btn_login'
    input_method_allow_id = 'android:id/button1'
    mi_vipaccount = {'username': '18751950965', 'password': 'Wpjsnj163.'}  # 我的小米账号
    miren_account = {'username': 'wupeng6', 'password': 'Indoor123.'}  # 小米人账号

    def __init__(self, deviceobject):
        self.device = deviceobject

    def handleGuidePage(self, private_logger):
        """
        处理引导页面
        """
        private_logger.info("handle Guide page now ...")
        private_logger.info("判断是否存在")
        t_exec = 0
        while True:
            self.device.set_screen_stay_on()
            if self.device.checkElementsIfNotExist(1, u'存储空间严重不足'):
                private_logger.info('存储空间严重不足弹框处理')
                self.device.click_element_by_ResourceId("android:id/button2")
            if self.device.checkElementsIfNotExist(1, u'确定'):
                self.device.click_element_by_Text("确定")
            if self.device.checkElementsIfNotExist(0, 'com.android.provision:id/img_miui'):
                private_logger.info('进入引导页进行跳过引导处理')
                self.device.quickJumpGuide()
                t_unscreenlock = 0
                while True:
                    if t_unscreenlock > 180:
                        return False
                    time.sleep(10)
                    if self.device.checkElementsIfNotExist(0, 'com.android.systemui:id/current_date'):
                        private_logger.info('第一次解锁，并等待初始化')
                        self.device.unScreenLock()
                        self.device.set_screen_stay_on()
                        # time.sleep(15)
                    if self.device.checkElementsIfNotExist(0, 'com.android.systemui:id/current_date'):
                        private_logger.info('第2次解锁')
                        self.device.unScreenLock()
                    private_logger.info('处理未发布机型，登录小米内部网络和登录我的小米账号')
                    self.handle_unpublish_devices()
                    if self.device.checkElementsIfNotExist(0, 'com.miui.home:id/icon_icon'):
                        return True
                    time.sleep(10)
                    t_unscreenlock += 1
            time.sleep(10)
            t_exec += 1
            if t_exec >= 180:
                return False

    def handle_unpublish_devices(self):
        if self.device.checkElementsIfNotExist(0, self.open_wifi_settings):
            self.device.click_element_by_ResourceId(self.open_wifi_settings)
            time.sleep(2)
            self.device.click_element_by_Text(self.wifi_name)
            self.device.sendText(self.wifi_username_input, self.miren_account['username'])
            time.sleep(1)
            self.device.sendText(self.wifi_password_input, self.miren_account['password'])
            self.device.click_element_by_ResourceId(self.wifi_submit)
            self.device.click_element_by_ResourceId(self.wifi_settings_page_back)
            time.sleep(3)
            if self.device.checkElementsIfNotExist(0, 'com.android.systemui:id/current_date'):
                self.device.unScreenLock()
            if self.device.checkElementsIfNotExist(0, self.verified_btn):
                self.device.click_element_by_ResourceId(self.verified_btn)
            self.device.click_element_by_ResourceId(self.mi_account_name)
            if self.device.checkElementsIfNotExist(0, self.input_method_allow_id):
                self.device.click_element_by_ResourceId(self.input_method_allow_id)
            self.device.sendText(self.mi_account_name, self.mi_vipaccount['username'])
            self.device.sendText(self.mi_account_password, self.mi_vipaccount['password'])
            self.device.click_element_by_ResourceId(self.mi_account_submit)
            time.sleep(5)
            if self.device.checkElementsIfNotExist(0, self.verified_btn):
                self.device.click_element_by_ResourceId(self.verified_btn)
            time.sleep(3)
            if self.device.checkElementsIfNotExist(0, 'com.android.systemui:id/current_date'):
                self.device.unScreenLock()

        # t_exec = 0
        # while True:
        #     if t_exec >= 5:
        #         self.device.quickJumpGuide()
        #         return True
        #     private_logger.info('检测是否进入mainpage')
        #     if self.device.checkElementsIfNotExist(0, 'com.miui.home:id/delete_zone'):
        #         break
        #     else:
        #         private_logger.info('点击>进行引导设置')
        #         if self.device.checkElementsIfNotExist(0, 'com.android.provision:id/img_miui'):
        #             self.device.click_element_by_ResourceId("com.android.provision:id/next")
        #         private_logger.info('进入语言选择页面，跳过')
        #         if self.device.checkElementsIfNotExist(1, u"设置语言"):
        #             self.device.click_element_by_ResourceId('com.android.provision:id/next')
        #         private_logger.info('进入连接互联网设置界面，跳过')
        #         if self.device.checkElementsIfNotExist(1, u"连接互联网"):
        #             self.device.click_element_by_ResourceId(
        #                 'com.android.settings:id/provision_btn_extra')
        #         private_logger.info('使用条款，点击继续')
        #         if self.device.checkElementsIfNotExist(1, u"使用条款"):
        #             self.device.click_element_by_ResourceId('com.android.provision:id/next')
        #         private_logger.info('检测sim卡，点击继续')
        #         if self.device.checkElementsIfNotExist(1, u"未检测到SIM卡"):
        #             self.device.click_element_by_ResourceId('com.android.provision:id/next')
        #         private_logger.info('设置手机密码，跳过')
        #         if self.device.checkElementsIfNotExist(1, u"设置手机密码"):
        #             self.device.click_element_by_ResourceId('com.android.provision:id/next')
        #         private_logger.info('设置语音唤醒，跳过')
        #         if self.device.checkElementsIfNotExist(1, u"设置语音唤醒"):
        #             self.device.click_element_by_ResourceId('com.android.provision:id/btn_skip')
        #         private_logger.info('其他设置，继续')
        #         if self.device.checkElementsIfNotExist(1, u"其他设置"):
        #             self.device.click_element_by_ResourceId('com.android.provision:id/next')
        #         private_logger.info('个性化风格，继续')
        #         if self.device.checkElementsIfNotExist(1, u"个性化风格"):
        #             self.device.click_element_by_ResourceId(
        #                 'com.android.thememanager:id/continue_btn')
        #         private_logger.info('系统导航方式设置，经典导航键，继续')
        #         if self.device.checkElementsIfNotExist(1, u"系统导航方式"):
        #             self.device.click_element_by_ResourceId(
        #                 'com.android.provision:id/radio_button_virtual_keys', 5)
        #             self.device.click_element_by_ResourceId('com.android.provision:id/next')
        #         private_logger.info('设置完毕，>')
        #         if self.device.checkElementsIfNotExist(1, u"体验激动人心的MIUI吧!"):
        #             self.device.click_element_by_ResourceId('com.android.provision:id/next')
        #     time.sleep(2)
        #     t_exec += 1
        # return True
